<template>
  <slot v-if="auth"></slot>
</template>
<script setup lang="ts">
/**
 * @description {Component} 组件级权限控制,用于控制组件的显示或隐藏。
 * @props {string[]} authority
 * @example :authority="['add','edit']"
 * @author 花东红
 * @time 2024/5/5
 */
type Props = {
  authority: string[]
}

const props = withDefaults(defineProps<Props>(), {
  authority: () => []
})

const { authority } = toRefs(props)

const router = useRouter()

const auth = computed(() => {
  const authButtonList = (router.currentRoute.value.meta.permission || []) as string[]
  const all_permission = '*:*:*'
  if (authority.value && authority.value instanceof Array && authority.value.length > 0) {
    return authButtonList.some(
      (permission: string) => all_permission === permission || authority.value.includes(permission)
    )
  }
  return false
})
</script>
<style lang="less" scoped></style>
